package com.iot.wlei.bmob_test;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputLayout;
import com.google.android.material.textview.MaterialTextView;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.edit_name)
    TextInputLayout editName;
    @BindView(R.id.edit_id)
    TextInputLayout editId;
    @BindView(R.id.edit_password)
    TextInputLayout editPassword;
    @BindView(R.id.edit_perm)
    TextInputLayout editPerm;
    @BindView(R.id.edit_tel)
    TextInputLayout editTel;
    @BindView(R.id.btn_reg)
    MaterialButton btnReg;
    @BindView(R.id.edit_objectid)
    TextInputLayout editObjectid;
    @BindView(R.id.btn_del)
    MaterialButton btnDel;
    @BindView(R.id.btn_query)
    MaterialButton btnQuery;
    @BindView(R.id.btn_updata)
    MaterialButton btnUpdata;
    @BindView(R.id.textview)
    MaterialTextView textview;
    @BindView(R.id.edit_where)
    TextInputLayout editWhere;
    @BindView(R.id.btn_go)
    MaterialButton btnGo;
    @BindView(R.id.pic)
    ImageView pic;
    private String APP_ID = "e0a08d79f7479529ed420f2fcdf42b33";
    private String Version = "v1.00"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Bmob.initialize(MainActivity.this, APP_ID);
    }

    @OnClick({R.id.btn_reg, R.id.btn_del, R.id.btn_query, R.id.btn_updata, R.id.btn_go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reg:
                Table_user MyUser = new Table_user();
                MyUser.setUserName(editName.getEditText().getText().toString());
                MyUser.setTelephone(editTel.getEditText().getText().toString());
                MyUser.setLoginPassword(editPassword.getEditText().getText().toString());
                MyUser.setUserId(editId.getEditText().getText().toString());
                MyUser.setUserPermission(editPerm.getEditText().getText().toString());

                MyUser.save(new SaveListener<String>() {
                    @Override
                    public void done(String ObjectId, BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "添加成功  ：" + ObjectId, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "访问失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.btn_del:
                Table_user table_user = new Table_user();
                table_user.setObjectId(editObjectid.getEditText().getText().toString());
                table_user.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "删除失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.btn_query:
                BmobQuery<Table_user> bmobQuery = new BmobQuery<Table_user>();
                bmobQuery.getObject(editObjectid.getEditText().getText().toString(), new QueryListener<Table_user>() {
                    @Override
                    public void done(Table_user table_user, BmobException e) {
                        if (e == null) {
                            editName.setHint(table_user.getUserName().toString());
                            editPassword.setHint(table_user.getLoginPassword().toString());
                            editId.setHint(table_user.getUserId().toString());
                            editPerm.setHint(table_user.getUserPermission().toString());
                            editTel.setHint(table_user.getTelephone().toString());
                        } else {
                            Toast.makeText(MainActivity.this, "查询失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.btn_updata:

                Table_user table_user1 = new Table_user();
                table_user1.setTelephone(editTel.getEditText().getText().toString());
                table_user1.update(editObjectid.getEditText().getText().toString(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "更新失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;

            case R.id.btn_go:

                BmobQuery<Table_user> bmobQuerywhere = new BmobQuery<>();
                bmobQuerywhere.addWhereEqualTo("userName", editWhere.getEditText().getText().toString());
                bmobQuerywhere.findObjects(new FindListener<Table_user>() {
                    @Override
                    public void done(List<Table_user> list, BmobException e) {
                        if (e == null) {
                            for (int i = 0; i < list.size(); i++) {
                                editTel.setHint(list.get(i).getTelephone());
                                editName.setHint(list.get(i).getUserName());
                                editId.setHint(list.get(i).getUserId());
                                editPassword.setHint(list.get(i).getLoginPassword());
                                editPerm.setHint(list.get(i).getUserPermission());

                                String url =  list.get(i).getUserPic();
                                Glide.with(MainActivity.this).load(url).into(pic);
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "查询失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
        }
    }

}
